package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.TrialApplicationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.TrialApplicationsForm;
import com.youlai.boot.factory.model.form.TrialApplicationsAuditForm;
import com.youlai.boot.factory.model.query.TrialApplicationsQuery;
import com.youlai.boot.factory.model.vo.TrialApplicationsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 试用申请单前端控制层
 *
 * @author youlaitech
 * @since 2025-11-11 10:19
 */
@Tag(name = "试用申请单接口")
@RestController
@RequestMapping("/api/v1/trial-applications")
@RequiredArgsConstructor
public class TrialApplicationsController  {

    private final TrialApplicationsService trialApplicationsService;

    @Operation(summary = "试用申请单分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:query')")
    public PageResult<TrialApplicationsVO> getTrialApplicationsPage(TrialApplicationsQuery queryParams ) {
        IPage<TrialApplicationsVO> result = trialApplicationsService.getTrialApplicationsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增试用申请单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:add')")
    public Result<Void> saveTrialApplications(@RequestBody @Valid TrialApplicationsForm formData ) {
        boolean result = trialApplicationsService.saveTrialApplications(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取试用申请单表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:edit')")
    public Result<TrialApplicationsForm> getTrialApplicationsForm(
        @Parameter(description = "试用申请单ID") @PathVariable Long id
    ) {
        TrialApplicationsForm formData = trialApplicationsService.getTrialApplicationsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "根据申请人的openid获取申请试用表单详情信息")
    @GetMapping("/getDetailByOpenId")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:edit')")
    public Result<TrialApplicationsForm> getTrialApplicationsDetailByOpenId() {
        TrialApplicationsForm formData = trialApplicationsService.getTrialApplicationsDetailByOpenId();
        return Result.success(formData);
    }

    @Operation(summary = "修改试用申请单")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:edit')")
    public Result<Void> updateTrialApplications(
            @Parameter(description = "试用申请单ID") @PathVariable Long id,
            @RequestBody @Validated TrialApplicationsForm formData
    ) {
        boolean result = trialApplicationsService.updateTrialApplications(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除试用申请单")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:delete')")
    public Result<Void> deleteTrialApplicationss(
        @Parameter(description = "试用申请单ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = trialApplicationsService.deleteTrialApplicationss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "申请试用表单（同一个openid只能新增一次，如果存在则改为修改，待审核状态不能修改）")
    @PostMapping("/apply")
    public Result<Void> applyTrialApplication(@RequestBody @Valid TrialApplicationsForm formData) {
        boolean result = trialApplicationsService.applyTrialApplication(formData);
        return Result.judge(result);
    }

    @Operation(summary = "审核试用申请单（只能编辑审核备注、审核状态和审核时间）")
    @PutMapping("/{id}/audit")
    @PreAuthorize("@ss.hasPerm('factory:trial-applications:audit')")
    public Result<Void> auditTrialApplication(
            @Parameter(description = "试用申请单ID") @PathVariable Long id,
            @RequestBody @Valid TrialApplicationsAuditForm auditForm
    ) {
        // 确保ID一致
        auditForm.setId(id);
        boolean result = trialApplicationsService.auditTrialApplication(id, auditForm);
        return Result.judge(result);
    }
}
